<?php

namespace app\common\model;

use basic\ModelBasic;
use think\Log;

class DistributionUser extends ModelBasic
{
	use \traits\ModelTrait;

    //直销订单
    const DIRECT_ORDER=1;
    //分销订单
    const FISSION_ORDER=2;
    //分销用户默认等级
    const DEFAULT_LEVEL=1;

    //protected $table = 'eb_user';
    public function user()
    {
    	// 设置预载入查询方式为IN方式
        return $this->belongsTo('app\common\model\User');
    }

    public function puser()
    {
        return $this->belongsTo('app\common\model\User','pid');
    }

    public function organ()
    {
        return $this->belongsTo("app\common\model\Organ",'org_id');
    }
    /**
     * 计算用户的分成比例
     */
    public function dividePercent($dSeparate){
        switch($this->level){
            case 0:
                return $dSeparate->senior_separate;
            case 1:
                return $dSeparate->first_separate;
            case 2:
                return $dSeparate->second_separate;
            case 3:
                return $dSeparate->third_separate;
            default:
                return 0;
        }
        return 0;
    }

    /**
     * 将用户表里的user_type转为role_id
     */
    public static function parseUserTypeToRoleId($user_type){
        switch($user_type){
            case 'parent':
                return 1;
            case 'teacher':
                return 3;
            case 'student':
                return 2;
        }
        return 1;
    }

    /**
     * 从用户创建分销用户
     */
    public static function createDUserFromUser($user_id,$org_id){
        $user=User::get($user_id);
        if ( empty($user) ) return false;

        $spreadUser=new DistributionUser;
        $spreadUser->org_id=$org_id;
        $spreadUser->user_id=$user_id;
        $spreadUser->mobile=$user->mobile;
        $spreadUser->role_id=self::parseUserTypeToRoleId($user->user_type);
        $spreadUser->level=self::DEFAULT_LEVEL;
        $spreadUser->balance=0;
        $spreadUser->total_com=0;
        $spreadUser->direct_com=0;
        $spreadUser->fission_com=0;
        $spreadUser->total_order_amount=0;
        $spreadUser->total_order_num=0;
        $spreadUser->pid=0;
        $spreadUser->save();
        $spreadUser->create_time=time();
        return $spreadUser;
    }

    /**
     * 获取佣金
     */
    public function getCommission($order_type,$order_id,$order_amount,$commission)
    {
        //添加分销记录
        $dc=new DistributionCommission();
        $dc->user_id=$this->user_id;
        $dc->org_id=$this->org_id;
        $dc->type=1;
        $dc->order_type=$order_type;
        $dc->order_id=$order_id;
        $dc->order_amount=round($order_amount,2);
        $dc->commission=round($commission,2);
        $dc->balance=round($this->balance+$commission,2);
        $dc->create_time=time();
        $dc->save();
        //调整用户总额度
        $this->balance=round($this->balance+$commission,2);
        $this->total_com=round($this->total_com+$commission,2);
        if($order_type==1){
            $this->direct_com=round($this->direct_com+$commission,2);
        }else{
            $this->fission_com=round($this->fission_com+$commission,2);
        }
        $this->total_order_num=$this->total_order_num+1;
        $this->total_order_amount=round($this->total_order_amount+$order_amount,2);
        $this->save();
    }
} //Class End
